﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class OrderNetworkData : BaseFunction
    {


        public OrderNetworkData()
            : base()
        {

        }

        public OrderNetworkData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetABFOLGE(DataSet dsTarget, DataRow dwABPOS)
        {
           
            string TableName = "ABFOLGE";
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT distinct ABFAUFKRS,ABFAUFLFD,ABFAUFPOS,trim(LSNAME1) as LSNAME1 "
                    + "  FROM ABFOLGE , LIEFDAT WHERE  ABFMANDANT = LSMANDANT " +
                    " AND ABFLIEFNR = LSLIEFNR AND TRIM(ABFLIEFNR) IS NOT NULL AND  " +
                     " ABFAUFKRS = :ABFAUFKRS AND ABFAUFLFD=:ABFAUFLFD AND ABFAUFPOS=:ABFAUFPOS";
                orlReadCommand.Parameters.Add("ABFAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                orlReadCommand.Parameters.Add("ABFAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                orlReadCommand.Parameters.Add("ABFAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = TableName;
                    dsResult.AcceptChanges();
                }
                 byte[] b;
                string value;

                foreach (DataRow rowABFOLGE in dsResult.Tables[TableName].Rows)
                {
                    
                    foreach (DataColumn dc in dsResult.Tables[TableName].Columns)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(rowABFOLGE[dc.ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        rowABFOLGE[dc.ColumnName] = value;

                    }
                    if (true)
                    {
                        dwABPOS["FACTORY"] = rowABFOLGE["LSNAME1"];
                    }
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    if (dsTarget.Tables.Contains(TableName))
                    {
                        dsTarget.Tables[TableName].Clear();
                    }
                    dsTarget = ImportDataTable(dsTarget, dtTemp);
                }
                dsTarget.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return dsTarget;
        }
   
        public DataSet GetABAUFM(DataSet dsTarget, DataRow dwVKPOS)
        {
           
            string TableName = "ABAUFM";
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT LEVEL,ABPAUFART,ABAAUFKRS,ABAAUFLFD,ABAAUFPOS,ABARKUNDNR, "
                    + " ABAVKAUFKRS||'-'||ABAVKAUFLFD||'-'||ABAVKAUFPOS AS ORDERNO,ABASANZSTK, "
                    + " DECODE(ABPAUFART,'GP',ABASSTKGEW,ABASSTKLG)/1000 AS QTY, ABARESQTY "
                    + " FROM ABAUFM ,ABPOS WHERE ABAAUFKRS = ABPAUFKRS AND ABAAUFLFD = ABPAUFLFD AND ABAAUFPOS = ABPAUFPOS "
                    + " CONNECT BY PRIOR ABAAUFKRS = ABAVKAUFKRS AND PRIOR ABAAUFLFD = ABAVKAUFLFD AND PRIOR ABAAUFPOS =  ABAVKAUFPOS "
                    + " START WITH ABAVKAUFKRS = :ABAVKAUFKRS AND ABAVKAUFLFD =:ABAVKAUFLFD AND ABAVKAUFPOS = :ABAVKAUFPOS "
                                           ;
                orlReadCommand.Parameters.Add("ABAVKAUFKRS", OracleType.NVarChar).Value = dwVKPOS["VKPAUFKRS"].ToString();
                orlReadCommand.Parameters.Add("ABAVKAUFLFD", OracleType.NVarChar).Value = dwVKPOS["VKPAUFLFD"].ToString();
                orlReadCommand.Parameters.Add("ABAVKAUFPOS", OracleType.NVarChar).Value = dwVKPOS["VKPAUFPOS"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = TableName;
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    if (dsTarget.Tables.Contains(TableName))
                    {
                        dsTarget.Tables[TableName].Clear();
                    }
                    dsTarget = ImportDataTable(dsTarget, dtTemp);
                }
                dsTarget.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return dsTarget;
            
        }
        
        

    }
}